package com.qili.util;

import com.qili.base.entity.BasePathConfig;
import com.qili.base.service.BasePathConfigService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.List;

@ConfigurationProperties
@Component
public class FilePathUtil {
    /**
     * 自定义文件路径
     */
    private static String uploadPath;
    private static String placeholder;

    @Value("${supervisor.uploadPath}")
    public void setUploadPath(String psUpPath) {
        uploadPath = psUpPath;
    }

    public static String getUploadPath(){
        return uploadPath;
    }

    @Value("${supervisor.placeholder}")
    public void setPlaceholder(String psHolder) {
        placeholder = psHolder;
    }

    /**
     * @param uploadType
     * @Author:Zhangshuo
     * @Description:根据文件类型获取存储路径
     * @Date:21:30 2020/11/17
     */
    public static String getFullSavePathAndNameByConfigFileId(UploadType uploadType) {
        String AsFileId = uploadType.getFilePathId();
        ApplicationContext context = SpringUtil.getApplicationContext();
        BasePathConfigService basePathConfigService = context.getBean(BasePathConfigService.class);
        BasePathConfig basePathConfig = basePathConfigService.selectByPrimaryKey(AsFileId);
        String AsConfigPath = basePathConfig.getBfcFilepath();
        AsConfigPath = replacePlaceHolderPath(AsConfigPath, uploadType);
        //文件主路径
        String AsMainPath = uploadPath;
        String AsPath = AsMainPath + AsConfigPath;
        File AfTmp = new File(AsPath);
        if (!AfTmp.exists()) {
            AfTmp.mkdirs();
        }
        String AsFileName = uploadType.getFileName();
        if (StringUtil.isNotBlank(AsFileName)) {
            AsPath = AsPath + File.separator + AsFileName;
        }
        return AsPath;
    }

    public static String getPackageTemplate(String asTemplateFile,String asFbCode){
        if(StringUtils.isNotBlank(asTemplateFile) && StringUtils.isNotBlank(asFbCode)
            && asTemplateFile.contains("[") && asTemplateFile.contains("]")){
            String pack = asTemplateFile.substring(asTemplateFile.lastIndexOf("["),asTemplateFile.lastIndexOf("]")+1);
            return asTemplateFile.replace(pack, asFbCode);
        }
        return asTemplateFile;
    }

    /**
     * @param uploadType
     * @Author:Zhangshuo
     * @Description:根据文件类型获取存储路径
     * @Date:21:30 2020/11/17
     */
    public static String getSavePathByFileType(UploadType uploadType) {
        System.out.println("67676767676");
        String AsFileId = uploadType.getFilePathId();
        ApplicationContext context = SpringUtil.getApplicationContext();
        BasePathConfigService basePathConfigService = context.getBean(BasePathConfigService.class);
        BasePathConfig basePathConfig = basePathConfigService.selectByPrimaryKey(AsFileId);
        System.out.println("88yyyyyyyyy88888888888888888");
        String AsConfigPath = basePathConfig.getBfcFilepath();
        AsConfigPath = replacePlaceHolderPath(AsConfigPath, uploadType);
        System.out.println("uuuuuuuuuuuu99999999999999");
        return AsConfigPath;
    }

    /**
     * @param psConfigPath
     * @param uploadType
     * @Author:Zhangshuo
     * @Description:替换项目和分包路径
     * @Date:11:38 2020/11/18
     */
    public static String replacePlaceHolderPath(String psConfigPath, UploadType uploadType) {
        String AsConfigPath = psConfigPath;
        List<String> AlstPlaceHolder = uploadType.getPlaceholder();
        System.out.println("iiiiiiiiiiii");
        String[] AyConfigHolders = placeholder.split(",");
        int AiCnt = AyConfigHolders.length;
        for (int i = 0; i < AiCnt; i++) {
            String AsPlaceHolder = AyConfigHolders[i];
            if (AlstPlaceHolder.size() > i) {
                String AsPlaceHolderValue = AlstPlaceHolder.get(i);
                System.out.println(AsPlaceHolderValue);
                if (StringUtil.isNotBlank(AsPlaceHolderValue) && (StringUtil.isNotBlank(AsPlaceHolder))) {
                    System.out.println("77777777777");
                    String AsConfigPathRep = AsConfigPath.replace(AsPlaceHolder, AsPlaceHolderValue);
                    AsConfigPath = AsConfigPathRep;
                }
            }
        }
        return AsConfigPath;
    }

    /**
     * @param psFileName
     * @Author:Zhangshuo
     * @Description:获取文件后缀名
     * @Date:15:45 2020-12-07
     */
    public static String getFileSufiix(String psFileName) {
        return psFileName.substring(psFileName.lastIndexOf("."));
    }

    /**
     * @param psFileName
     * @Author:Zhangshuo
     * @Description:获取文件后缀名
     * @Date:15:45 2020-12-07
     */
    public static String getFileName(String psFileName) {
        return psFileName.substring(psFileName.lastIndexOf(File.separator) + 1, psFileName.length());
    }

    /**
     * @param pId
     * @Author:Zhangshuo
     * @Description:获取模板文件
     * @Date:14:27 2020-12-09
     */
    public static String getPathConfigTemplateFile(String pId) {
        ApplicationContext context = SpringUtil.getApplicationContext();
        BasePathConfigService basePathConfigService = context.getBean(BasePathConfigService.class);
        BasePathConfig basePathConfig = basePathConfigService.selectByPrimaryKey(pId);
        return basePathConfig.getBfcTemplateFile();
    }

    /**
     * @param pFilePath
     * @Author:Zhangshuo
     * @Description:
     * @Date:17:36 2020-12-14
     */
    public static String doFileSeparator(String pFilePath) {
        return pFilePath.replace("\\", File.separator);
    }

}
